package com.gpframework.module.controller.process;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.process.domain.GpProcessIncomingInspect;
import com.gpframework.module.process.domain.req.GpProcessIncomingInspectAdd;
import com.gpframework.module.process.domain.req.GpProcessIncomingInspectReq;
import com.gpframework.module.process.domain.req.GpProcessIncomingInspectUpdate;
import com.gpframework.module.process.domain.vo.GpProcessIncomingInspectVo;
import com.gpframework.module.process.service.IGpProcessIncomingInspectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpProcessIncomingInspectController
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 加工来料检验单Controller
 */
@Validated
@Api(tags = "加工来料检验单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/process/inspect" )
public class GpProcessIncomingInspectController extends BaseController {

    private final IGpProcessIncomingInspectService iGpProcessIncomingInspectService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-20
  * @Param gpProcessIncomingInspect
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('process:inspect:list')")
    @GetMapping("/list")
    public TableDataInfo<GpProcessIncomingInspectVo> list(GpProcessIncomingInspectReq gpProcessIncomingInspectReq)
    {
        startPage();
        QueryWrapper<GpProcessIncomingInspect> lqw = new QueryWrapper<GpProcessIncomingInspect>();
        gpProcessIncomingInspectReq.generatorQuery(lqw,true);
        List<GpProcessIncomingInspectVo> list = iGpProcessIncomingInspectService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('process:inspect:add')" )
    @Log(title = "添加加工来料检验单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpProcessIncomingInspectAdd gpProcessIncomingInspectAdd) {
        GpProcessIncomingInspect gpProcessIncomingInspect = Convert.convert(new TypeReference<GpProcessIncomingInspect>() {}, gpProcessIncomingInspectAdd);
        return toAjax(iGpProcessIncomingInspectService.save(gpProcessIncomingInspect) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('process:inspect:edit')" )
    @Log(title = "修改加工来料检验单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpProcessIncomingInspectUpdate> gpIncomingInspectUpdateList) {
//        GpProcessIncomingInspect gpProcessIncomingInspect = Convert.convert(new TypeReference<GpProcessIncomingInspect>() {}, gpProcessIncomingInspectUpdate);
        return toAjax(iGpProcessIncomingInspectService.updateByWeightAndWidthAndYardVerify(gpIncomingInspectUpdateList) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessIncomingInspect
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('process:inspect:export')" )
    @Log(title = "导出加工来料检验单" , businessType = BusinessType.EXPORT)
    @ApiOperation("导出加工来料检验单")
    @GetMapping("/export" )
    public AjaxResult export(GpProcessIncomingInspectReq gpProcessIncomingInspectReq) {
        QueryWrapper<GpProcessIncomingInspect> lqw = new QueryWrapper<GpProcessIncomingInspect>();
        gpProcessIncomingInspectReq.generatorQuery(lqw,true);
        List<GpProcessIncomingInspectVo> list = iGpProcessIncomingInspectService.listVo(lqw);
        ExcelUtil<GpProcessIncomingInspectVo> util = new ExcelUtil<GpProcessIncomingInspectVo>(GpProcessIncomingInspectVo. class);
        return util.exportExcel(list, "inspect" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('process:inspect:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpProcessIncomingInspectVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpProcessIncomingInspectService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('process:inspect:remove')" )
    @Log(title = "删除加工来料检验单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpProcessIncomingInspectService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 生成甲醛表
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("生成甲醛表")
    @PreAuthorize("@ss.hasPermi('manufacture:createFormaldehyde:add')" )
    @Log(title = "生成甲醛表" , businessType = BusinessType.INSERT)
    @PostMapping("/createFormaldehyde")
    public AjaxResult createFormaldehyde(@Valid @RequestBody GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        if (gpProcessIncomingInspectUpdate == null || CollectionUtils.isEmpty(gpProcessIncomingInspectUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProcessIncomingInspectService.createFormaldehyde(gpProcessIncomingInspectUpdate) ? 1 : 0);
    }


    /**
     * @Description 改标
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("改标")
    @PreAuthorize("@ss.hasPermi('manufacture:createOverflowRate:add')" )
    @Log(title = "改标" , businessType = BusinessType.INSERT)
    @PostMapping("/createChangeLabel")
    public AjaxResult createChangeLabel(@Valid @RequestBody GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        if (gpProcessIncomingInspectUpdate == null || CollectionUtils.isEmpty(gpProcessIncomingInspectUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProcessIncomingInspectService.createChangeLabel(gpProcessIncomingInspectUpdate) ? 1 : 0);
    }

    /**
     * @Description 送检
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("送检")
    @PreAuthorize("@ss.hasPermi('manufacture:createSendTest:add')" )
    @Log(title = "送检" , businessType = BusinessType.INSERT)
    @PostMapping("/createSendTest")
    public AjaxResult createSendTest(@Valid @RequestBody GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        if (gpProcessIncomingInspectUpdate == null || CollectionUtils.isEmpty(gpProcessIncomingInspectUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProcessIncomingInspectService.createSendTest(gpProcessIncomingInspectUpdate) ? 1 : 0);
    }


    /**
     * @Description 生产加工问题追踪表
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessScheduling
     * @Return AjaxResult
     */
    @ApiOperation("生产加工问题追踪表")
    @PreAuthorize("@ss.hasPermi('manufacture:createProductionProcessingTracking:add')" )
    @Log(title = "生产加工问题追踪表" , businessType = BusinessType.INSERT)
    @PostMapping("/createProductionProcessingTracking")
    public AjaxResult createProcessingIncomingInspection(@Valid @RequestBody GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        if (gpProcessIncomingInspectUpdate == null || CollectionUtils.isEmpty(gpProcessIncomingInspectUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProcessIncomingInspectService.createProductionProcessingTracking(gpProcessIncomingInspectUpdate) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-20
     * @Param gpProcessIncomingInspect
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('process:inspect:add')" )
    @Log(title = "复制" , businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        if (gpProcessIncomingInspectUpdate == null || gpProcessIncomingInspectUpdate.getId() == null || gpProcessIncomingInspectUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpProcessIncomingInspectUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpProcessIncomingInspectService.copy(gpProcessIncomingInspectUpdate.getId(), gpProcessIncomingInspectUpdate.getCopyNum()) ? 1 : 0);
    }
}
